/*
 * Copyright (c) 2010,2012,2014 Apple Inc. All Rights Reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 */

#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>
#include <Security/SecCMS.h>
#include <Security/SecInternal.h>
#include <Security/SecPolicyPriv.h>
#include <CommonCrypto/CommonDigest.h>
#include "shared_regressions.h"


const uint8_t root_ca[] = {
  0x30, 0x82, 0x03, 0x59, 0x30, 0x82, 0x02, 0x41, 0xa0, 0x03, 0x02, 0x01,
  0x02, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x44, 0x31, 0x0b, 0x30,
  0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
  0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x0a, 0x41, 0x70, 0x70,
  0x6c, 0x65, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x31, 0x20, 0x30, 0x1e, 0x06,
  0x03, 0x55, 0x04, 0x03, 0x13, 0x17, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67,
  0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x41, 0x75, 0x74, 0x68,
  0x6f, 0x72, 0x69, 0x74, 0x79, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x30, 0x30,
  0x39, 0x32, 0x39, 0x32, 0x32, 0x31, 0x30, 0x31, 0x38, 0x5a, 0x17, 0x0d,
  0x31, 0x30, 0x31, 0x30, 0x32, 0x39, 0x32, 0x32, 0x31, 0x30, 0x31, 0x38,
  0x5a, 0x30, 0x44, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06,
  0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  0x0a, 0x13, 0x0a, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x49, 0x6e, 0x63,
  0x2e, 0x31, 0x20, 0x30, 0x1e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x17,
  0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f,
  0x6e, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x69, 0x74, 0x79, 0x30,
  0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30,
  0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xab, 0xb2, 0x30, 0x5a,
  0xf7, 0x0b, 0xe5, 0xb1, 0xcf, 0xd8, 0x21, 0xe8, 0x13, 0x0a, 0x73, 0x31,
  0x5e, 0x8d, 0xbb, 0xf8, 0xb4, 0xcf, 0x7a, 0x6c, 0xb3, 0xa4, 0x57, 0x7b,
  0xa3, 0xea, 0x0c, 0x89, 0xe3, 0x4f, 0x03, 0x41, 0x02,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c1, 0xc2, 0x07,
  0x51, 0x63, 0x5b, 0x3d, 0x77, 0x79, 0xc9, 0x8b, 0xae, 0xdd, 0x16, 0x21,
  0x7d, 0xc6, 0x3d, 0xa2, 0x73, 0x80, 0x9c, 0xb1, 0x27, 0x81, 0x65, 0x92,
  0x33, 0xd9, 0xda, 0xf0, 0xe1, 0xc4, 0x7a, 0x2d, 0xbf, 0xe0, 0x76, 0x0d,
  0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x56, 0x30, 0x54, 0x30, 0x12, 0x06,
  0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x08, 0x30, 0x06, 0x01,
  0x01, 0xff, 0x02, 0x01, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
  0x04, 0x16, 0x04, 0x14, 0x78, 0x2f, 0x9f, 0x48, 0x53, 0x25, 0xe3, 0x8c,
  0x2f, 0xaf, 0x1d, 0x21, 0x86, 0xdc, 0xb0, 0x50, 0x93, 0xa9, 0x24, 0xc2,
  0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
  0x14, 0x78, 0x2f, 0x9f, 0x48, 0x53, 0x25, 0xe3, 0x8c, 0x2f, 0xaf, 0x1d,
  0x21, 0x86, 0xdc, 0xb0, 0x50, 0x93, 0xa9, 0x24, 0xc2, 0x30, 0x0d, 0x06,
  0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00,
  0x03, 0x82, 0x01, 0x01, 0x00, 0x5b, 0x89, 0x64, 0x90, 0xa4, 0x22, 0x05,
  0x55, 0xd7, 0x31, 0x26, 0xd7, 0x38, 0x22, 0x23, 0xac, 0xd6, 0x35, 0xe4,
  0x0e, 0xfd, 0xb2, 0x53, 0x6c, 0xb8, 0x22, 0x32, 0x91, 0xdf, 0xd1, 0xfc,
  0x70, 0xb6, 0xa0, 0x2d, 0xa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2, 0xa5, 0x79, 0x32, 0x60, 0xb8, 0xef, 0xcd,
  0x89, 0x0d, 0xab, 0x18, 0x9b, 0x06, 0xf9, 0xab, 0xac, 0xc1, 0xfb, 0xe3,
  0xf0, 0xf3, 0x5c, 0xc6, 0x87, 0x2e, 0xf3, 0xec, 0x2b, 0x88, 0x37, 0xaa,
  0x32, 0x76, 0x33, 0xf9, 0xa2, 0xb9, 0x2e, 0xc1, 0x11
};

unsigned char signed_urlbag[] = {
  0x30, 0x82, 0x04, 0xe8, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  0x01, 0x07, 0x02, 0xa0, 0x82, 0x04, 0xd9, 0x30, 0x82, 0x04, 0xd5, 0x02,
  0x01, 0x01, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02,
  0x1a, 0x05, 0x00, 0x30, 0x14, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  0x0d, 0x01, 0x07, 0x01, 0xa0, 0x07, 0x04, 0x05, 0x66, 0x6f, 0x6f, 0x0d,
  0x0a, 0xa0, 0x82, 0x03, 0x04, 0x30, 0x82, 0x03, 0x00, 0x30, 0x82, 0x01,
  0xe8, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x01, 0x01, 0x30, 0x0d, 0x06,
  0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00,
  0x30, 0x44, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0a,
  0x13, 0x0a, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x49, 0x6e, 0x63, 0x2e,
  0x31, 0x20, 0x30, 0x1e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x17, 0x43,
  0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e,
  0x20, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x69, 0x74, 0x79, 0x30, 0x1e,
  0x17, 0x0d, 0x31, 0x30, 0x30, 0x39, 0x32, 0x39, 0x32, 0x32, 0x31, 0x30,
  0x31, 0x38, 0x5a, 0x17, 0x0d, 0x31, 0x30, 0x31, 0x30, 0x32, 0x39, 0x32,
  0x32, 0x31, 0x30, 0x31, 0x38, 0x5a, 0x30, 0x4f, 0x31, 0x0b, 0x30, 0x09,
  0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30,
  0x11, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x0a, 0x41, 0x70, 0x70, 0x6c,
  0x65, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x31, 0x2b, 0x30, 0x29, 0x06, 0x03,
  0x55, 0x04, 0x03, 0x13, 0x22, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75,
  0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x61, 0x70, 0x70, 0x6c, 0x65,
  0x2e, 0x63, 0x6f, 0x6d, 0x20, 0x62, 0x61, 0x67, 0x20, 0x73, 0x69, 0x67,
  0x6e, 0x65, 0x72, 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d,
  0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xb5, 0x4d, 0xea, 0xa2,
  0xfa, 0x13, 0x1d, 0x80, 0x3d, 0xe5, 0x9d, 0x2b, 0x2f, 0x92, 0xe9, 0xff,
  0x51, 0x2c, 0xef, 0x09, 0x15, 0x12, 0x72, 0x48, 0x12, 0x87, 0xa9, 0xe4,
  0x8e, 0x4a, 0x37, 0x46, 0x47, 0xa1, 0xe4, 0x72, 0xb1, 0xba, 0x8f, 0xd5,
  0xd2, 0x26, 0x8c, 0x1b, 0xf2, 0x0e, 0x36, 0xb0, 0x71, 0xa0, 0x2a, 0x23,
  0xb6, 0x3b, 0x27, 0x36, 0x8d, 0x1d, 0xa9, 0xc0, 0xba, 0x82, 0xcc, 0x7d,
  0x97, 0xbe, 0xa5, 0x49, 0x08, 0x26, 0x84, 0x7e, 0x99, 0x55, 0x05, 0x75,
  0xc8, 0x9c, 0xd0, 0xa2, 0x1b, 0x9b, 0x86, 0x82, 0xd8, 0x51, 0xd1, 0xf9,
  0x37, 0xee, 0xac, 0x8a, 0xe3, 0x59, 0xc5, 0xcf, 0x22, 0x5e, 0x95, 0x20,
  0x47, 0x48, 0x85, 0x67, 0xb2, 0xe7, 0x9c, 0x8a, 0xc3, 0x01, 0xbe, 0xf1,
  0x27, 0x41, 0x4f, 0x70, 0x21, 0x11, 0xff, 0x09, 0x5a, 0x08, 0x14, 0x0b,
  0xed, 0xdd, 0x81, 0x13, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x76, 0x30,
  0x74, 0x30, 0x16, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x01, 0x01, 0xff, 0x04,
  0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03,
  0x03, 0x30, 0x1a, 0x06, 0x03, 0x55, 0x1d, 0x20, 0x01, 0x01, 0xff, 0x04,
  0x10, 0x30, 0x0e, 0x30, 0x0c, 0x06, 0x0a, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  0x63, 0x64, 0x05, 0x05, 0x01, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
  0x04, 0x16, 0x04, 0x14, 0x30, 0x0f, 0xe5, 0x40, 0x51, 0xc8, 0x26, 0x61,
  0x3a, 0xba, 0xa1, 0xd0, 0xc0, 0x61, 0x84, 0x97, 0xa6, 0x46, 0xbd, 0x50,
  0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
  0x14, 0x78, 0x2f, 0x9f, 0x48, 0x53, 0x25, 0xe3, 0x8c, 0x2f, 0xaf, 0x1d,
  0x21, 0x86, 0xdc, 0xb0, 0x50, 0x93, 0xa9, 0x24, 0xc2, 0x30, 0x0d, 0x06,
  0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00,
  0x03, 0x82, 0x01, 0x01, 0x00, 0x60, 0x72, 0x78, 0xf5, 0x1e, 0x3f, 0x30,
  0x7c, 0x3a, 0xf5, 0xd6, 0xe2, 0x01, 0x62, 0x3b, 0x0e, 0x5d, 0xa1, 0xbb,
  0x77, 0xb1, 0x68, 0x53, 0xdb, 0x9b, 0x43, 0x5e, 0x82, 0xd4, 0xd7, 0xb7,
  0x72, 0xa1, 0x24, 0xce, 0x9e, 0x16, 0x3e, 0x91, 0xc7, 0xb0, 0x56, 0xd0,
  0xe2, 0xd7, 0x86, 0x04, 0x46, 0x15, 0x87, 0xf8, 0xd1, 0x3c, 0x0c, 0xd7,
  0xdf, 0x6f, 0xbb, 0x58, 0xc0, 0xf2, 0xde, 0x38, 0x0e, 0x20, 0xcc, 0x2b,
  0x3a, 0x64, 0xee, 0x5c, 0x59, 0x15, 0x05, 0x57, 0x8b, 0x8c, 0xf8, 0x19,
  0xc0, 0x1d, 0x0f, 0x8d, 0x11, 0xf9, 0x77, 0xc4, 0x90, 0x2b, 0x63, 0x1c,
  0x5c, 0x36, 0x36, 0x2d, 0x99, 0xa6, 0x4c, 0x0f, 0xad, 0x85, 0xab, 0x26,
  0x38, 0x19, 0x86, 0xf8, 0x1b, 0xd1, 0x4a, 0x1e, 0xcf, 0x0e, 0x91, 0x5a,
  0x26, 0x8d, 0x91, 0xc7, 0x2f, 0x5c, 0x53, 0x0e, 0x0c, 0x2b, 0xf9, 0xd0,
  0x03, 0xf3, 0x0b, 0xb8, 0x85, 0x67, 0x8b, 0xfc, 0x56, 0x1f, 0x6e, 0x2b,
  0xa0, 0x20, 0x81, 0xe6, 0xd6, 0xc7, 0x1b, 0x68, 0xf6, 0x7a, 0xdb, 0x27,
  0x95, 0x0d, 0xfd, 0x03, 0xd0, 0x1c, 0x95, 0x31, 0x8f, 0x9d, 0x26, 0xe1,
  0x30, 0xf1, 0xf5, 0x6f, 0xe2, 0xb6, 0x4a, 0x3c, 0x43, 0xfd, 0x02, 0xd1,
  0x86, 0x1e, 0x70, 0x71, 0xeb, 0xeb, 0x76, 0x6e, 0xb2, 0x17, 0x10, 0x9e,
  0x78, 0x83, 0xb9, 0xff, 0x39, 0xa1, 0xeb, 0xf3, 0x63, 0xd6, 0x21, 0xeb,
  0x6e, 0x27, 0xee, 0x79, 0x02, 0x44, 0x8f, 0xd2, 0x3c, 0x3f, 0x81, 0x74,
  0x4a, 0x8a, 0xb7, 0x6d, 0x6a, 0x5c, 0x4f, 0x90, 0x58, 0x4b, 0x79, 0xdd,
  0x80, 0xe3, 0xa6, 0x05, 0xe7, 0x65, 0xd8, 0x6d, 0xb6, 0xe0, 0x6c, 0xdc,
  0xad, 0xc5, 0x61, 0x16, 0x0b, 0xb6, 0x9e, 0x1e, 0x89, 0x43, 0x3e, 0x93,
  0x8d, 0x1b, 0x8a, 0x42, 0xa0, 0x00, 0xa8, 0x60, 0x61, 0x31, 0x82, 0x01,
  0xa3, 0x30, 0x82, 0x01, 0x9f, 0x02, 0x01, 0x01, 0x30, 0x49, 0x30, 0x44,
  0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x0a,
  0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x31, 0x20,
  0x30, 0x1e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x17, 0x43, 0x6f, 0x6e,
  0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x41,
  0x75, 0x74, 0x68, 0x6f, 0x72, 0x69, 0x74, 0x79, 0x02, 0x01, 0x01, 0x30,
  0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0xa0, 0x81,
  0xb1, 0x30, 0x18, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
  0x09, 0x03, 0x31, 0x0b, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  0x01, 0x07, 0x01, 0x30, 0x1c, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  0x0d, 0x01, 0x09, 0x05, 0x31, 0x0f, 0x17, 0x0d, 0x31, 0x30, 0x30, 0x39,
  0x32, 0x39, 0x32, 0x33, 0x33, 0x34, 0x33, 0x39, 0x5a, 0x30, 0x23, 0x06,
  0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04, 0x31, 0x16,
  0x04, 0x14, 0x85, 0x54, 0x26, 0x06, 0x8e, 0xe8, 0x93, 0x9d, 0xf6, 0xbc,
  0xe2, 0xc2, 0xc4, 0xb1, 0xe7, 0x34, 0x65, 0x32, 0xa1, 0x33, 0x30, 0x52,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x0f, 0x31,
  0x45, 0x30, 0x43, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  0x0d, 0x03, 0x07, 0x30, 0x0e, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  0x0d, 0x03, 0x02, 0x02, 0x02, 0x00, 0x80, 0x30, 0x0d, 0x06, 0x08, 0x2a,
  0x86, 0x48, 0x86, 0xf7, 0x0d, 0x03, 0x02, 0x02, 0x01, 0x40, 0x30, 0x07,
  0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x07, 0x30, 0x0d, 0x06, 0x08, 0x2a,
  0x86, 0x48, 0x86, 0xf7, 0x0d, 0x03, 0x02, 0x02, 0x01, 0x28, 0x30, 0x0d,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05,
  0x00, 0x04, 0x81, 0x80, 0x5a, 0xc8, 0x72, 0x37, 0xe4, 0x7a, 0xdd, 0xde,
  0x47, 0x6e, 0x27, 0x4c, 0x73, 0x70, 0x27, 0x01, 0xf3, 0x2c, 0x52, 0x1f,
  0xa2, 0xa5, 0xbd, 0xd4, 0x6b, 0x6e, 0x44, 0x1a, 0xfc, 0x43, 0x55, 0xbc,
  0xe5, 0x9a, 0xdb, 0x06, 0x1c, 0x67, 0x49, 0xb7, 0x06, 0x45, 0x98, 0xd8,
  0x72, 0xc3, 0xc9, 0x6e, 0x47, 0xc8, 0x29, 0x33, 0xbd, 0x05, 0x2c, 0x9f,
  0x74, 0x13, 0x2f, 0x57, 0x30, 0x86, 0x07, 0x08, 0xfd, 0xea, 0x38, 0x7f,
  0xec, 0xcd, 0x47, 0x64, 0xfb, 0xea, 0x60, 0x6e, 0xea, 0xc6, 0xd4, 0x57,
  0x46, 0xe3, 0x71, 0xc3, 0xa4, 0xfc, 0x7a, 0x2c, 0xed, 0x6b, 0xe7, 0x7f,
  0x4c, 0xe7, 0x24, 0x8f, 0x9c, 0xd9, 0x9a, 0xa7, 0xdc, 0xf1, 0xc8, 0x20,
  0x59, 0xd1, 0x1d, 0x26, 0xb9, 0xd3, 0x19, 0x21, 0x68, 0x86, 0x76, 0x6e,
  0xb8, 0xd8, 0x42, 0x31, 0x2a, 0x32, 0x8d, 0x8b, 0x66, 0xce, 0x65, 0x2c
};
unsigned int signed_urlbag_len = 1260;

static void tests(void)
{
    CFDataRef message = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, signed_urlbag, signed_urlbag_len, kCFAllocatorNull);
    CFMutableDataRef munged_message = CFDataCreateMutableCopy(kCFAllocatorDefault, 0, message);
    is(*(CFDataGetMutableBytePtr(munged_message) + 59), '\015', "modifying right byte");
    *(CFDataGetMutableBytePtr(munged_message) + 59) = '\012';

    SecPolicyRef policy = NULL;
    SecTrustRef trust = NULL;
    SecTrustResultType result;
    CFDataRef root_ca_data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, root_ca, sizeof(root_ca), kCFAllocatorNull);
    SecCertificateRef anchor = SecCertificateCreateWithData(kCFAllocatorDefault, root_ca_data);
    CFArrayRef anchors = CFArrayCreate(kCFAllocatorDefault, (const void **)&anchor, 1, &kCFTypeArrayCallBacks);
    CFRelease(anchor);
    CFReleaseNull(root_ca_data);

    policy = SecPolicyCreateURLBag();
    ok_status(SecCMSVerifySignedData(message, NULL, policy, &trust, NULL, NULL, NULL), "validate message");

    SecTrustSetAnchorCertificates(trust, anchors);
    ok_status(SecTrustEvaluate(trust, &result), "evaluate trust");
    ok(result == kSecTrustResultUnspecified, "private root");

    CFReleaseNull(trust);

    is_status(errSecAuthFailed, SecCMSVerifySignedData(munged_message, NULL, policy, &trust, NULL, NULL, NULL), "validate message");

    CFReleaseNull(trust);

    CFReleaseNull(policy);
    CFReleaseNull(anchors);
    CFReleaseNull(message);
    CFReleaseNull(munged_message);
}

int si_65_cms_cert_policy(int argc, char *const *argv)
{
	plan_tests(5);

	tests();

	return 0;
}



/*

# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	Makefile
#	extensions.txt
#
echo x - Makefile
sed 's/^X//' >Makefile << 'END-of-Makefile'
X
Xall:
X	@echo "No default action"
X
XCA-key.pem:
X	@openssl genrsa -out CA-key.pem 2048
X
XCA-csr.pem: CA-key.pem
X	@openssl req -new -key CA-key.pem -sha1 -days 3650 -subj "/C=US/O=Apple Inc./CN=Configuration Authority" -nodes -out CA-csr.pem
X
XCA-cert.pem: CA-csr.pem CA-key.pem
X	@openssl x509 -req -sha1 -in CA-csr.pem -signkey CA-key.pem -set_serial 0 -out CA-cert.pem -extensions authority -extfile extensions.txt
X
Xleaf-cert.pem: CA-cert.pem CA-key.pem
X	@openssl req -newkey rsa:1024 -sha1 -days 3650 -subj "/C=US/O=Apple Inc./CN=configuration.apple.com bag signer" -nodes -out leaf-csr.pem -keyout leaf-key.pem
X	@openssl x509 -req -sha1 -in leaf-csr.pem -CA CA-cert.pem -CAkey CA-key.pem -out leaf-cert.pem -set_serial 1 -extfile extensions.txt -extensions leaf
X
Xcheck: CA-cert.pem leaf-cert.pem
X	@echo Validate leaf
X	@openssl verify -CAfile CA-cert.pem leaf-cert.pem
X	@echo Output SHA-1 for private root
X	@openssl x509 -in CA-cert.pem -outform DER | openssl dgst -binary -sha1 | xxd -i
X	@echo Display leaf
X	@openssl x509 -noout -text -in leaf-cert.pem
X
Xsigned-urlbag: CA-cert.pem leaf-cert.pem content
X	@cat leaf-cert.pem leaf-key.pem > leaf.pem
X	@openssl smime -sign -aes128 -outform der -nodetach -signer leaf.pem -CAfile CA-cert.pem -in content -out signed-urlbag
X
Xclean:
X	@rm -f leaf-cert.pem leaf-csr.pem leaf-key.pem leaf.pem signed-urlbag
X
Xreal-clean:
X	@rm -f CA-key.pem CA-csr.pem CA-cert.pem leaf-cert.pem leaf-csr.pem leaf-key.pem leaf.pem signed-urlbag
END-of-Makefile
echo x - extensions.txt
sed 's/^X//' >extensions.txt << 'END-of-extensions.txt'
X[authority]
XbasicConstraints=critical,CA:true,pathlen:0
XsubjectKeyIdentifier=hash
XauthorityKeyIdentifier=keyid
X
X[leaf]
XextendedKeyUsage=critical,codeSigning
XcertificatePolicies=critical,1.2.840.113635.100.5.5.1
XsubjectKeyIdentifier=hash
XauthorityKeyIdentifier=keyid
X
X[req]
Xdistinguished_name     = req_distinguished_name
X
X[req_distinguished_name]
END-of-extensions.txt
exit

*/
